// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dengues.core.warehouse.IWarehouseNode;
import org.dengues.warehouse.models.WarehouseNode;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.TreeItem;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-3-25 qiang.zhang $
 * 
 */
public class WarehouseTreeViewer extends TreeViewer {

    private Map<String, Boolean> expanded = new HashMap<String, Boolean>();

    /**
     * Qiang.Zhang.Adolf@gmail.com WarehouseTreeView constructor comment.
     * 
     * @param parent
     * @param style
     */
    public WarehouseTreeViewer(Composite parent, int style) {
        super(parent, style);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.TreeViewer#getExpanded(org.eclipse.swt.widgets.Item)
     */
    @Override
    protected boolean getExpanded(Item item) {
        WarehouseNode warehouseNode = getWarehouseNode(item);
        if (warehouseNode != null) {

        }
        return super.getExpanded(item);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getWarehouseNode".
     * 
     * @param item
     */
    private WarehouseNode getWarehouseNode(Item item) {
        Object data = item.getData();
        if (data instanceof WarehouseNode) {
            return (WarehouseNode) data;
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.AbstractTreeViewer#setExpandedState(java.lang.Object, boolean)
     */
    public void setExpandedState(Object element, boolean expanded) {
        TreeItem[] items = this.getIdenticalNodes(element, true);
        if (items != null && items.length > 0) {
            if (expanded) {
                createChildren(items[0]);
            }
            this.setExpanded(items[0], expanded);
        } else {
            TreeItem[] nodes = this.getIdenticalNodes(element, false);
            if (nodes != null && nodes.length > 0) {
                super.setExpandedState(nodes[0].getData(), expanded);
            }
        }
    }

    public void refresh() {
        this.getControl().getDisplay().syncExec(new Runnable() {

            public void run() {
                WarehouseTreeViewer.super.refresh();
            }
        });
    }

    public TreeItem[] getIdenticalNodes(Object sample, boolean exact) {
        if (sample != null) {
            return this.findUnfreshNodes(this.getTree().getItems(), sample, exact);
        }
        return null;
    }

    protected TreeItem[] findUnfreshNodes(TreeItem[] items, Object obj, boolean exact) {
        List retVal = this.findUnfreshNodesImpl(items, obj, exact);
        return retVal == null ? null : (TreeItem[]) retVal.toArray(new TreeItem[retVal.size()]);
    }

    protected List findUnfreshNodesImpl(TreeItem[] items, Object obj, boolean exact) {
        if (items != null) {
            List retVal = new ArrayList();
            for (int i = 0; i < items.length; i++) {
                List tmp = this.findUnfreshNodes(items[i], obj, exact);
                if (tmp != null) {
                    retVal.addAll(tmp);
                }
            }
            return retVal;
        }
        return null;
    }

    protected List findUnfreshNodes(TreeItem item, Object obj, boolean exact) {
        Object data = item.getData();
        if (obj == data || !exact && obj.equals(data)) {
            return Arrays.asList(new TreeItem[] { item });
        }
        if (data instanceof IWarehouseNode) {
            IWarehouseNode dataNode = (IWarehouseNode) data;
            if (obj == dataNode || !exact && obj.equals(dataNode)) {
                return Arrays.asList(new TreeItem[] { item });
            }
        }
        return this.findUnfreshNodesImpl(item.getItems(), obj, exact);
    }
}
